package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.BompNoTxnRegMapper;
import cc.rengu.igas.bomp.common.entity.NoTxnReg;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhangxuran on 2020/04/18
 */
public class BompNoTxnRegMapperImpl implements BompNoTxnRegMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.CSMC_POOL_NAME);

    @Override
    public List<NoTxnReg> selectBompNoTxnRegByMchntNo(String instId, String mchntNo, String batchDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompNoTxnRegByMchntNo");
        NoTxnReg noTxnReg = new NoTxnReg();
        noTxnReg.setInstId(instId);
        noTxnReg.setMchntNo(mchntNo);
        noTxnReg.setBatchDate(batchDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, noTxnReg, NoTxnReg.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户无交易二维码或终端信息失败,instId:<{}>,mchntNo:<{}>,batchDate:<{}>", instId, mchntNo, batchDate);
            return null;
        }
        return objectList.stream().map(item -> (NoTxnReg) item).collect(Collectors.toList());
    }
}
